import request from '@/common/request/index.js'
import utils from '@/common/utils/index.js'
import store from '@/common/store/index.js'
import { BASE_URL, SIGN } from '@/env.js'
const jweixin = require('jweixin-module')

// 微信公众号分享
const wxoaShare = () => {
    // #ifdef H5
    const { app, user } = store.state
    const spm = user.userInfo ? this.id : ""

    let title = app.config || "";
    let imgUrl = app.config || "";
    let desc = app.config || "";

    const uri = window.location.origin + window.location.pathname
    const curi = window.location.hash.indexOf('/pages/video/play')
    let link = ''
    if (curi != -1) {
        link = `${window.location.href}&scene=${spm}`
        if (utils.getUrlParams().d) {
            const params = JSON.parse(decodeURIComponent(utils.getUrlParams().d))
            title = params|| ""
            imgUrl = params||  ""
            desc = params ||""
        }
    } else {
        link = `${uri}?${SIGN}#/pages/home/index?scene=${spm}`
    }

    request('user.share', { uri }).then(res => {
		debugger;
            if (res.code === 1) {
                jweixin.config({
                    debug: false,
                    appId: res.data.appId,
                    nonceStr: res.data.nonceStr,
                    timestamp: res.data.timestamp,
                    signature: res.data.signature,
                    jsApiList: [
                        'updateAppMessageShareData',
                        'updateTimelineShareData',
                    ]
                })
                jweixin.ready(() => {
                    // true(已登录) false(未登录)
                    store.commit('app/setJwx', spm ? true : false)
                        // 分享给朋友
                    jweixin.updateAppMessageShareData({
                            title,
                            desc,
                            link,
                            imgUrl,
                            success: e => {
                                console.log("分享好友");
                            }
                        })
                        // 分享朋友圈
                    jweixin.updateTimelineShareData({
                        title,
                        link,
                        imgUrl,
                        success: e => {
                            console.log("分享朋友圈");
                        }
                    })
                })
                jweixin.error(e => {
                    console.log(e, '微信分享config信息验证失败');
                })
            }
        })
        // #endif
}

export default wxoaShare